package com.wswy.wyjk.ui.share;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.my.httpapi.api.baseUtils.Toast;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX.Req;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.wswy.wyjk.R;
import com.wswy.wyjk.ui.uitls.ChannelManager;
import com.wswy.wyjk.utils.AdverConfigUtil;
import com.wswy.wyjk.utils.ToastUtils;

public class ShareHelper {
    public static final String TAG = "ShareHelper";
    public static String WX_APP_ID = "wx0df36c8c5c84cc5d";
    public static String WX_APP_Secret_key = "a7bfcece5209375ff8dbe7124e43494f";

    private static IWXAPI api;

    private static final String share_title = "12123驾考-春运出行防疫政策指南";
    private static final String share_desc = "抗击疫情，12123驾考助您快速拿驾照，科一科四驾校2021年新版精简题库。";
    private static final String share_title_huawei = "交通驾考题库-春运出行防疫政策指南";
    private static final String share_desc_huawei = "抗击疫情，交通驾考题库助您快速拿驾照，科一科四驾校2021年新版精简题库。";
    private static final String share_icon = "";

    public static final String shareUrl = "http://activity.zqwzc.cn/sftr/index?platform=12123jktk";


    /**
     * 好友
     */
    public static final int TYPE_WX_FRIEND = Req.WXSceneSession;

    /**
     * 朋友圈
     */
    public static final int TYPE_WX_TIMELINE = Req.WXSceneTimeline;

//    static {
//        try {
//            api = WXAPIFactory.createWXAPI(AppContext.getContext(), WX_APP_ID, true);
//            api.registerApp(WX_APP_ID);
//        } catch (Exception e) {
//            Log.e(TAG, "wechat registerApp ex--->", e);
//        }
//    }


    public static String login() {
        if (!api.isWXAppInstalled()) {
            ToastUtils.showText("未安装微信");
            return null;
        }
        String state = "wzcx_auth_" + System.currentTimeMillis();
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = state;
        if (api.sendReq(req)) {
            return state;
        }
        return null;
    }

    public static void shareApp(final Context context) {

        new ShareDialog(context, new ShareDialog.OnChooseShareTypeCallback() {
            @Override
            public void onChooseType(int shareType) {
                shareWeb((Activity) context, shareType == TYPE_WX_FRIEND ? SHARE_MEDIA.WEIXIN : SHARE_MEDIA.WEIXIN_CIRCLE);
            }
        }).show();
    }


    /**
     * 分享链接
     */
    public static void shareWeb(final Activity activity, SHARE_MEDIA platform) {
        int localPic;
        UMWeb web = new UMWeb(AdverConfigUtil.homeBannerLinkUrl);//连接地址
        if ("huawei".equals(ChannelManager.getChannel(activity))) {
            web.setTitle(share_title_huawei);//标题
            web.setDescription(share_desc_huawei);//描述
            localPic = R.mipmap.share_huawei_logo;
        } else {
            web.setTitle(share_title);//标题
            web.setDescription(share_desc);//描述
            localPic = R.mipmap.share_logo;
        }

        if (TextUtils.isEmpty(share_icon)) {
            web.setThumb(new UMImage(activity, localPic));  //本地缩略图
        } else {
            web.setThumb(new UMImage(activity, share_icon));  //网络缩略图
        }
        new ShareAction(activity)
                .setPlatform(platform)
                .withMedia(web)
                .setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                    }

                    @Override
                    public void onResult(final SHARE_MEDIA share_media) {
                        // activity.runOnUiThread(() -> Toast.show("分享成功"));
                    }

                    @Override
                    public void onError(final SHARE_MEDIA share_media, final Throwable throwable) {

                        activity.runOnUiThread(() -> Toast.show("分享失败"));
                    }

                    @Override
                    public void onCancel(final SHARE_MEDIA share_media) {
                        // activity.runOnUiThread(() -> Toast.show("分享取消"));
                    }
                }).share();
    }
}
